﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using System.Web.Script.Serialization;
namespace Setup
{

    public class AppInfo
    {
        public InstallInfo InstallInfo { get; set; }
    }

    [Serializable]
    /// <summary>
    /// 安装信息
    /// </summary>
    public class InstallInfo
    {
        public InstallInfo()
        {

        }
        /// <summary>
        /// 安装唯一标识
        /// </summary>
        public string SoftWareKey { get; set; }
        /// <summary>
        /// 安装路径
        /// </summary>
        public string InstallLocation { get; set; }
        /// <summary>
        /// 显示名称
        /// </summary>
        public string DisplayName { get; set; }
        /// <summary>
        /// 显示版本
        /// </summary>
        public string DisplayVersion { get; set; }

        /// <summary>
        /// 卸载指令
        /// </summary>
        [System.Web.Script.Serialization.ScriptIgnore]
        public string UninstallString { get { return InstallLocation + "Setup.exe"; } set { } }

        [System.Web.Script.Serialization.ScriptIgnore]
        public List<UpdateFile> Files { get; set; }

        [System.Web.Script.Serialization.ScriptIgnore]
        public List<Link> links { get; set; }

        /// <summary>
        /// 主程序
        /// </summary>
        [System.Web.Script.Serialization.ScriptIgnore]
        public string Main { get; set; }

        /// <summary>
        /// 公司名
        /// </summary>
        [System.Web.Script.Serialization.ScriptIgnore]
        public string Corporation { get; set; }
        [System.Web.Script.Serialization.ScriptIgnore]
        public List<string> CloseProcesss { get; set; }

        public void Save(string XmlFileName = "setup.xml")
        {
            System.Web.Script.Serialization.JavaScriptSerializer xml = new System.Web.Script.Serialization.JavaScriptSerializer();
            if (File.Exists(XmlFileName))
                File.Delete(XmlFileName);
            File.AppendAllText(XmlFileName, xml.Serialize(this));
        }

        public static InstallInfo Open(String XmlFileName = @"/setup.xml")
        {

            if (System.IO.File.Exists(XmlFileName))
            {

                System.Web.Script.Serialization.JavaScriptSerializer xml = new System.Web.Script.Serialization.JavaScriptSerializer();
                return xml.Deserialize<InstallInfo>(File.ReadAllText(XmlFileName));
            }
            return null;
        }
    }

    [Serializable]
    /// <summary>
    /// 下载文件
    /// </summary>
    public class UpdateFile
    {
        public string name { get; set; }
        public string md5 { get; set; }
        public string url { get; set; }
    }

    [Serializable]
    /// <summary>
    /// 快捷方式
    /// </summary>
    public class Link
    {
        /// <summary>
        /// 源文件
        /// </summary>
        public string sourceName { get; set; }

        /// <summary>
        /// 目标位置
        /// </summary>
        [System.Web.Script.Serialization.ScriptIgnore]
        public string destination { get { return Environment.GetFolderPath(Folder) + "\\" + DisplayName; } }

        public Environment.SpecialFolder Folder
        {
            get; set;
        }

        public string DisplayName { get; set; }

    }


    public class ExeVersion
    {
        public string Num { get; set; }

        public string Name { get; set; }

        public List<UpdateFile> Files { get; set; }

        public string FilesJson { get; set; }
    }
}
